package org.brownflat.watergate.dao;

import org.brownflat.watergate.domain.Branch;
import org.brownflat.watergate.domain.Commit;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.springframework.stereotype.Repository;

import java.util.List;

import static org.hibernate.criterion.Restrictions.eq;

/**
 * <p>&copy Rupert Jones 2012</p>
 *
 * @author rup
 */
@Repository
public class CommitDAO extends AbstractHibernateDAO<Commit> {

    public CommitDAO() {
        super(Commit.class);
    }

    @SuppressWarnings("unchecked")
    public Commit findLastCommitFor(Branch branch) {
        Criteria criteria = getCriteria().add(eq("branch", branch)).addOrder(Order.desc("revision")).setMaxResults(1);
        Object result = criteria.uniqueResult();
        return (Commit) result;
    }

    public List<Commit> findNotReviewTriggeredFor(Branch branch) {
        Criteria criteria = getCriteria().add(eq("branch", branch)).add(eq("reviewTriggerScanned", false));
        return criteria.list();
    }
}
